<script lang="ts" setup>
import { ref } from 'vue'
import { GetData } from '@/api/modules/hexiao'

const router = useRouter()
const findFrom = ref({
  Trano: '',
  OpenId: '',
  Mobile: '',
  Flag: '~',
  Method: '~',
  time: [],
})

const dataList: any = reactive({
  list: [],
})
// const router = useRouter()
const loading = ref(true)
const total = ref(0)
let PageSize = 20
// const viewType = ref(false)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.Trano && ({ Trano: findFrom.value.Trano })),
    ...(findFrom.value.OpenId && ({ OpenId: findFrom.value.OpenId })),
    ...(findFrom.value.Mobile && ({ Mobile: findFrom.value.Mobile })),
    ...(findFrom.value.Flag !== '~' && ({ Flag: findFrom.value.Flag })),
    ...(findFrom.value.Method !== '~' && ({ Method: findFrom.value.Method })),
    ...(findFrom.value.time.length !== 0 && ({
      StartAddTime: findFrom.value.time[0],
      EndAddTime: findFrom.value.time[1],
    })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

function LookOrder(orderNo: string) {
  router.push({
    name: 'RetailOrder',
    query: {
      orderNo,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
}

onMounted(async () => {
  GetDataList()
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="核销单号">
                <el-input v-model="findFrom.Trano" placeholder="请输入核销单号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="核销人OpenId">
                <el-input v-model="findFrom.OpenId" placeholder="请输入核销人OpenId" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="核销人手机号">
                <el-input v-model="findFrom.Mobile" placeholder="请输入核销人手机号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="类型">
                <el-select v-model="findFrom.Flag" placeholder="请选择" style="width: 240px;">
                  <el-option label="全部" value="~" />
                  <el-option label="核销券" :value="1" />
                  <el-option label="核销订单" :value="2" />
                </el-select>
              </el-form-item>
              <el-form-item label="核销方式">
                <el-select v-model="findFrom.Method" placeholder="请选择" style="width: 240px;">
                  <el-option label="全部" value="~" />
                  <el-option label="小程序" :value="1" />
                  <el-option label="客户端" :value="2" />
                </el-select>
              </el-form-item>
              <el-form-item label="核销时间">
                <el-date-picker
                  v-model="findFrom.time"
                  type="datetimerange"
                  range-separator="到"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <!-- <el-button type="primary" @click="viewType = !viewType">
          切换
        </el-button> -->
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="trano" label="核销单号" width="300">
          <template #default="scope">
            <span class="cursor-pointer" @click="LookOrder(scope.row.trano)">{{ scope.row.trano }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="openId" label="核销人OpenId" width="300" />
        <el-table-column align="center" prop="mobile" label="核销人手机号" width="200" />
        <el-table-column align="center" prop="flag" label="类型" width="160">
          <template #default="scope">
            <el-tag>
              {{ scope.row.flag === 1 ? '核销券'
                : scope.row.flag === 2 ? '核销订单' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="desp" label="备注" min-width="300" />
        <el-table-column align="center" prop="addTime" label="核销时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="修改时间" width="200" />
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
